// detail仓库
import { reqGoodsInfo, reqAddOrUpdateShopCart } from '@/api';
import { getUUID } from '@/utils/uuid_token';

const state = {
    goodInfo: {},
    uuid_token: getUUID(), //游客的临时身份
};
const mutations = {
    GETGOODINFO(state, goodInfo) {
        state.goodInfo = goodInfo;
    }
};
const actions = {
    // 获取产品信息
    async getGoodInfo({ commit }, skuId) {
        let result = await reqGoodsInfo(skuId);
        if (result.code == 200) {
            commit('GETGOODINFO', result.data)
        }
    },
    // 将产品添加到购物车中
    async addOrUpdateShopCart({ commit }, { skuId, skuNum }) {
        let result = await reqAddOrUpdateShopCart(skuId, skuNum);
        console.log(result);
        // 返回加入购物车成功还是失败信息
        if (result.code == 200) {
            return 'ok';
        } else {
            return Promise.reject(new Error('fail'));
        }
    }
};
const getters = {
    // 路径导航数据简化
    // categoryView(state) {
    //     return state.goodInfo.categoryView || {};
    // },
    // 产品信息数据简化
    info(state) {
        return state.goodInfo.info || {};
    },
    // 产品售卖属性数据简化
    saleAttr(state) {
        return state.goodInfo.saleAttr || [];
    },
    images(state) {
        return state.goodInfo.images || [];
    }
};
export default {
    state,
    mutations,
    actions,
    getters
}